package cn.dong.statistics.task;

import cn.dong.statistics.domain.Daily;
import cn.dong.statistics.service.DailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class StatisticsTask {

    @Autowired
    private DailyService dailyService;

    /**
     * 自动更新统计数据，每日更新一次
     */
//    @Scheduled(cron = "0 0 1 * * ? ")
    @Scheduled(cron = "0/10 * * * * ? ")
    public void uploadStatistics() {
        // 每日自动定时刷新，更新数据统计
        String time = new DateTime().minusDays(1).toString("yyyy-MM-dd");

        // 判断一下今天是否已经统计了数据
        QueryWrapper<Daily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", time);
        int count = dailyService.count(wrapper);
        if (count > 0) {
            // 删除之前保存的统计
            dailyService.remove(wrapper);
            log.info("\n删除之前保存的统计，更新数据统计：" + time);
        }
        dailyService.updateStatisticsInfo(time);
        log.info("\n每日自动定时刷新，更新数据统计：" + time);
    }


}
